# -*- coding:utf-8 -*-
 
import threading
import time
from datetime import datetime
from datetime import date
from stock import *
from crypto import *
from mailsender import *

def testFun1():
    print ("testFun1")

def testFun2():
    print ("testFun2")

# 定义要执行的处理函数
def fetch_Astock(thread_name):
    mainlist = make_main_list() 
    stocklist = make_stock_list()
    while True:
        white_stock(mainlist,stocklist)
        print ("stock fetch done")
        time.sleep(10)

def fetch_crypto(thread_name):
    while True:
        write_crypto()
        print ("Crypto fetch done")
        time.sleep(10)



# 可调度函数池
FUNC_MAP = {
    'send_email': send_email,
    'test_Fun2': testFun2
    # 你可以继续添加别的函数
}

# 配置表，可以改为外部文件/数据库
schedule_config = []
'''
schedule_config = [
    {'time': '00:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '00:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '00:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '00:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '00:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '01:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '01:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '01:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '01:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '01:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '02:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '02:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '02:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '02:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '02:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '03:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '03:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '03:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '03:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '03:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '04:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '03:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '03:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '03:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '03:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},


    {'time': '05:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '05:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '05:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '05:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '05:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '06:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '06:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '06:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '06:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '06:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '07:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '07:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '07:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '07:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '07:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '08:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '08:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '08:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '08:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '08:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '09:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '09:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '09:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '09:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '09:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '10:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '10:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '10:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '10:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '10:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '11:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '11:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '11:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '11:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '11:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '12:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '12:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '12:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '12:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '12:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '13:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '13:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '13:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '13:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '13:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '14:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '14:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '14:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '14:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '14:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '15:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '15:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '15:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '15:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '15:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '16:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '16:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '16:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '16:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '16:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '17:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '17:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '17:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '17:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '17:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '18:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '18:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '18:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '18:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '18:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '19:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '19:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '19:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '19:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '19:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '20:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '20:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '20:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '20:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '20:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '21:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '21:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '21:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '21:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '21:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '22:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '22:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '22:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '22:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '22:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

    {'time': '23:14', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist),['belfast.jpg']]},
    {'time': '23:07', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '23:22', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '23:38', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},
    {'time': '23:52', 'func': 'send_email', 'args': ["Crypto price analyze", check_crypto_tec(cryptolist,'15m'),['belfast.jpg']]},

]
'''

def scheduler(thread_name):
    print(f"[{threading.current_thread().name}] Scheduler started")
    executed = set()  # 记录已执行过的任务（防止重复）格式："func@YYYY-MM-DD HH:MM"
    last_clear_date = date.today()


    while True:
        now = datetime.now()
        fmt_time = now.strftime('%H:%M')
        for conf in schedule_config:
            # 构建唯一标识
            execute_key = f"{conf['func']}@{now.strftime('%Y-%m-%d')} {conf['time']}"
            # 时间匹配且没执行过
            if fmt_time == conf['time'] and execute_key not in executed:
                func = FUNC_MAP.get(conf['func'])
                if func is not None:
                    try:
                        print(f"[{threading.current_thread().name}] Executing {conf['func']} at {fmt_time}")
                        func(*conf['args'])
                        executed.add(execute_key)
                    except Exception as e:
                        print(f"[{threading.current_thread().name}] ERROR executing {conf['func']}: {e}")
                else:
                    print(f"[{threading.current_thread().name}] Function `{conf['func']}` not found")

        if date.today() != last_clear_date:
            executed.clear()
            last_clear_date = date.today()
        time.sleep(1)  # 一秒检查一次


# 主函数内容
if __name__ == "__main__":
    #创建线程
    thread1 = threading.Thread(target=fetch_Astock, args=("AstockThread",))
    thread2 = threading.Thread(target=fetch_crypto, args=("CryptoThread",))
    thread3 = threading.Thread(target=scheduler, args=("SchedulerThread",))
    # 启动线程
    thread1.start()
    thread2.start()
    thread3.start()
    # 主线程等待子线程完成
    thread1.join()
    thread2.join()
    thread3.join()
